import org.apache.spark.SparkConf
import org.apache.spark.sql.{SaveMode, SparkSession}


object rat_num {
  def main(args: Array[String]): Unit = {
    val sparkconf = new SparkConf()
      .setMaster("local[*]")
    val sparksession = SparkSession
      .builder()
      .config(sparkconf)
      .enableHiveSupport()
      .appName("price_rat")
      .getOrCreate()

    var df = sparksession.sql(
      """
        |SELECT
        |    CASE
        |        WHEN star_rating < 30 THEN '30以下'
        |        WHEN star_rating < 35 THEN '30-34'
        |        WHEN star_rating < 40 THEN '35-39'
        |        WHEN star_rating < 45 THEN '40-44'
        |        WHEN star_rating < 50 THEN '45-49'
        |        WHEN star_rating = 50 THEN '50'
        |    END AS star,
        |    SUM(consume_count) AS total_consume_count
        |FROM
        |    db_minsu.tb_minsu
        |GROUP BY
        |    CASE
        |        WHEN star_rating < 30 THEN '30以下'
        |        WHEN star_rating < 35 THEN '30-34'
        |        WHEN star_rating < 40 THEN '35-39'
        |        WHEN star_rating < 45 THEN '40-44'
        |        WHEN star_rating < 50 THEN '45-49'
        |        WHEN star_rating = 50 THEN '50'
        |    END
        |""".stripMargin
    )
    val rat_view = df.createTempView("rat_view")
    val rat_num = sparksession.sql("select star,total_consume_count from rat_view ")
    //dws_cardio_age.show()
    //-将结果进行存储，存储到 hive 数据库中

    rat_num.write.mode(SaveMode.Overwrite).saveAsTable("db_minsu.rat_num")



    sparksession.stop()
    sparksession.close()
  }
}
